package system;

import java.util.ArrayList;

public class Board {
	ArrayList<Edge> edges = new ArrayList<Edge>();
	ArrayList<ActionPoint> points = new ArrayList<ActionPoint>();
	Tile[][] bord = new Tile[11][8]; // [x,Y]
	
	
	public Tile[][] getBord() {
		return bord;
	}


	public Board(){
		setup();
	}


	private void setup() {
		bord[0][0] = new Tile(1.5, 1.5,null);
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 11; j++) {
				
				double x = 1.5;
				double y = i+1.5;
				
				try{
				x = bord[j-1][0].x + 0.75;	
				}catch(Exception e){}
				
				
				
				if (j%2 == 1)
						y-= 0.5;
				
				
				bord[j][i] = new Tile(x, y,null);
			}
		}
		
		bord[5][2].setTile(Tiles.SHEEP);
		bord[4][2].setTile(Tiles.SHEEP);
		bord[4][3].setTile(Tiles.SHEEP);
		bord[5][6].setTile(Tiles.SHEEP);
		
		bord[6][2].setTile(Tiles.WOOD);
		bord[3][4].setTile(Tiles.WOOD);
		bord[4][4].setTile(Tiles.WOOD);
		bord[6][5].setTile(Tiles.WOOD);
		
		bord[7][3].setTile(Tiles.ERTS);
		bord[5][3].setTile(Tiles.ERTS);
		bord[6][4].setTile(Tiles.ERTS);
		
		bord[6][3].setTile(Tiles.CLAY);
		bord[5][5].setTile(Tiles.CLAY);
		bord[4][5].setTile(Tiles.CLAY);
		
		bord[7][4].setTile(Tiles.GRAIN);
		bord[7][5].setTile(Tiles.GRAIN);
		bord[3][3].setTile(Tiles.GRAIN);
		bord[5][4].setTile(Tiles.GRAIN);
		bord[3][5].setTile(Tiles.GRAIN);
	
		bord[2][2].setTile(Tiles.WATER);
		bord[2][3].setTile(Tiles.WATER);
		bord[2][4].setTile(Tiles.WATER);
		bord[2][5].setTile(Tiles.WATER);
		bord[3][6].setTile(Tiles.WATER);
		bord[4][6].setTile(Tiles.WATER);
		bord[5][7].setTile(Tiles.WATER);
		bord[6][6].setTile(Tiles.WATER);
		bord[7][6].setTile(Tiles.WATER);
		bord[8][5].setTile(Tiles.WATER);
		bord[8][4].setTile(Tiles.WATER);
		bord[8][3].setTile(Tiles.WATER);
		bord[8][2].setTile(Tiles.WATER);
		bord[7][2].setTile(Tiles.WATER);
		bord[6][1].setTile(Tiles.WATER);
		bord[5][1].setTile(Tiles.WATER);
		bord[4][1].setTile(Tiles.WATER);
		bord[3][2].setTile(Tiles.WATER);
		
		setupEdges();
		
		
	}
	
	public void setupEdges(){
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 11; j++) {
				if (bord[j][i].getTile()!=null){
				
				double x = bord[j][i].getX();
				double y = bord[j][i].getY();
				
				//add points
				ActionPoint p1 = new ActionPoint(x-0.5, y);
				if (!points.contains(p1))
					points.add(p1);
				bord[j][i].addPoint(getPoint(p1));
				
				
				ActionPoint p2 = new ActionPoint(x-0.25, y-0.5);
				if (!points.contains(p2))
					points.add(p2);
				bord[j][i].addPoint(getPoint(p2));
				
				ActionPoint p3 = new ActionPoint(x+0.25, y-0.5);
				if (!points.contains(p3))
					points.add(p3);
				bord[j][i].addPoint(getPoint(p3));
				
				ActionPoint p4 = new ActionPoint(x+0.5, y);
				if (!points.contains(p4))
					points.add(p4);
				bord[j][i].addPoint(getPoint(p4));
				
				ActionPoint p5 = new ActionPoint(x+0.25, y+0.5);
				if (!points.contains(p5))
					points.add(p5);
				bord[j][i].addPoint(getPoint(p5));
				
				ActionPoint p6 = new ActionPoint(x-0.25, y+0.5);
				if (!points.contains(p6))
					points.add(p6);
				bord[j][i].addPoint(getPoint(p6));
				
				getPoint(p1).addTile(bord[j][i]);
				getPoint(p2).addTile(bord[j][i]);
				getPoint(p3).addTile(bord[j][i]);
				getPoint(p4).addTile(bord[j][i]);
				getPoint(p5).addTile(bord[j][i]);
				getPoint(p6).addTile(bord[j][i]);
				
				// set edges
				Edge e1 = new Edge(p1, p2);
				if (!edges.contains(e1))
					edges.add(e1);
				
				
				Edge e2 = new Edge(p2, p3);
				if (!edges.contains(e2))
					edges.add(e2);
				
				Edge e3 = new Edge(p3, p4);
				if (!edges.contains(e3))
					edges.add(e3);
				
				Edge e4 = new Edge(p4, p5);
				if (!edges.contains(e4))
					edges.add(e4);
				
				Edge e5 = new Edge(p5, p6);
				if (!edges.contains(e5))
					edges.add(e5);
				
				Edge e6 = new Edge(p6, p1);
				if (!edges.contains(e6))
					edges.add(e6);
				
				if (bord[j][i].getTile() != Tiles.WATER){
					getEdge(e1).setStreet();
					getEdge(e2).setStreet();
					getEdge(e3).setStreet();
					getEdge(e4).setStreet();
					getEdge(e5).setStreet();
					getEdge(e6).setStreet();
					System.out.println("not water");
				}
				
				}
				
				
				}
			}
	}
	
	
	public ArrayList<Edge> getEdges() {
		return edges;
	}
	
	public Edge getEdge(Edge e){
		for (Edge ed : edges) {
			if (ed.equals(e))
				return ed;
		}
		return e;
	}


	public ArrayList<ActionPoint> getPoints() {
		return points;
	}


	public ArrayList<ActionPoint> getNeighbours(ActionPoint ap) {
		ArrayList<ActionPoint> nb = new ArrayList<ActionPoint>();
		for (Edge e : getEdges(ap)) {
			if (e.getP1().equals(ap))
				nb.add(e.getP2());
				else
				nb.add(e.getP1());
		}
		return nb;
	}
	
	public ArrayList<Edge> getEdges(ActionPoint p){
		ArrayList<Edge> connections = new ArrayList<Edge>();
		for (Edge e : edges) {
			if (e.hasPoint(p))
				connections.add(e);
		}
		return connections;
	}
	
	
	/**
	 * 
	 * checks if a point already exists and if it does return its original
	 * 
	 * @param p
	 */
	public ActionPoint getPoint(ActionPoint p){
		for (ActionPoint  ap : points) {
			if (ap.equals(p))
				return ap;
		}
		return p;
	}
	
	
	
	public ActionPoint getPointAt(double x, double y){
		
		for (ActionPoint  ap : points) {
			if (Math.abs(ap.getX()-x) <0.2 && Math.abs(ap.getY()-y) <0.2 )
				return ap;
		}
		
		return null;
		
		
	}
	
	
	public static void main(String args[]){
		Board b = new Board();
		System.out.println(b.getEdges());
	}
}
